const url_all = {
	'DEV': `http://jianzujijian.cjzxkj.com`, // 测试 	dtkj.cjzxkj.com  https://dtkj.cjzxkj.com
	'ONI': `https://jifentongji.cjzxkj.com` //jianzujijian.cjzxkj.com
}
export let BASEURL = url_all['ONI']

export const request = (options = {}) => {
	return new Promise((resolve, rejects) => {
		handleRequest(options, resolve, rejects)
	})
}

// 发起请求
function handleRequest(options, resolve, reject) {
	options.data.token = uni.getStorageSync('token') || '';
	options.data.user_id =  uni.getStorageSync('userId') || 0;
	uni.request({
		url: BASEURL +'/api/'+ options.url,
		timeout: 120000,
		method: options.method,
		header: {
			Accept: uni.getStorageSync('token') || 'eb7ddc4139cbfa76c08fe2130964c4fc'
		},
		data: options.data,
		success: (response) => {
			switch (response.data.code) {
				case 500:
					uni.showToast({
						icon: 'error',
						position: 'top',
						title: `${response.data.msg}`
					});
					reject(response.data);
					break;
				case 0:
					uni.showModal({
						title:'提示',
						showCancel:false,
						content: `${response.data.msg}`
					});
					reject(response.data);
					break;
				case -200:
					uni.showModal({
						title:'提示',
						showCancel:false,
						content: `${response.data.msg}`
					});
					reject(response.data);
					break;
				case 1:
					resolve(response.data)
					break;
				case 401:
					uni.showModal({
						title:'登录失效',
						content:'请重新登录',
						showCancel:false,
						success({confirm}){
							if (confirm){
								uni.reLaunch({
									url:'/pages/login/login'
								})
							}
						}
					});
					reject(response.data);
					break;
				default:
					uni.showModal({
						title:'提示',
						showCancel:false,
						content: `${response.data.msg}`
					});
					reject(response.data);

			}
		},
		fail: (err) => {
			return reject(err);
		},
		complete() {
			uni.hideLoading();
		}
	})
}
